package org.jetlinks.pro.logging.excel;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.reactor.excel.CellDataType;
import org.hswebframework.reactor.excel.ExcelHeader;
import org.hswebframework.web.bean.FastBeanCopier;
import org.jetlinks.pro.logging.access.SerializableAccessLog;
import org.jetlinks.pro.logging.system.SerializableSystemLog;
import reactor.core.publisher.Flux;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author gyl
 * @time 2021/7/16 13:45
 */

@Getter
@Setter
@Slf4j
public class AccessLoggerExcelInfo {
    //ip
    String ip;
    //请求路径
    String url;
    //请求方式
    String httpMethod;
    //说明
    String action;
    //请求时间
    String requestTime;
    //请求耗时
    long timeConsuming;
    //请求用户
    String username;
    //类名
    String target;
    //方法名
    String method;
    //请求头
    String httpHeaders = "";
    //请求参数
    String parameters = "";

    public AccessLoggerExcelInfo(SerializableAccessLog accessLog) {
        this.ip = accessLog.getIp();
        this.url = accessLog.getUrl();
        this.httpMethod = accessLog.getHttpMethod();
        this.action = accessLog.getAction();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.requestTime = sdf.format(new Date(Long.parseLong(String.valueOf(accessLog.getRequestTime()))));
        this.timeConsuming = accessLog.getResponseTime() - accessLog.getRequestTime();
        this.username = accessLog.getContext().get("username");
        this.target = accessLog.getTarget();
        this.method = accessLog.getMethod();
        for (String key : accessLog.getHttpHeaders().keySet()) {
            this.httpHeaders += "【" + key + accessLog.getHttpHeaders().get(key) + "】";
        }
        for (String key : accessLog.getParameters().keySet()) {
            this.parameters += "【" + key + accessLog.getParameters().get(key) + "】";
        }
    }

    public static List<ExcelHeader> getLoggerHeaderMapping() {
        return new ArrayList<>(Arrays.asList(
                new ExcelHeader("ip", "ip", CellDataType.STRING),
                new ExcelHeader("url", "请求路径", CellDataType.STRING),
                new ExcelHeader("httpMethod", "请求方式", CellDataType.STRING),
                new ExcelHeader("action", "说明", CellDataType.STRING),
                new ExcelHeader("requestTime", "请求时间", CellDataType.STRING),
                new ExcelHeader("timeConsuming", "请求耗时", CellDataType.STRING),
                new ExcelHeader("username", "请求用户", CellDataType.STRING),
                new ExcelHeader("target", "类名", CellDataType.STRING),
                new ExcelHeader("method", "方法名", CellDataType.STRING),
                new ExcelHeader("httpHeaders", "请求头", CellDataType.STRING),
                new ExcelHeader("parameters", "请求参数", CellDataType.STRING)
        ));
    }

    public static Flux<AccessLoggerExcelInfo> getLoggerContentMapping(Flux<SerializableAccessLog> logs) {
        return logs.flatMap(log -> Flux.just(new AccessLoggerExcelInfo(log)))
                   .doOnError(err -> log.error(err.getMessage(), err));
    }

    public Map<String, Object> toMap() {
        HashMap<String, Object> copy = FastBeanCopier.copy(this, new HashMap<>(11));
        return copy;
    }
}

